package com.kara.woodAgent.agent.tool.provider;

import dev.langchain4j.agent.tool.ToolExecutionRequest;
import dev.langchain4j.agent.tool.ToolSpecification;
import dev.langchain4j.mcp.client.McpClient;
import dev.langchain4j.service.tool.ToolExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author tzy
 * @version 1.0
 * @project wood_agent
 * @description
 * @date 2025/6/4 09:56:04
 */
public class McpToolExecutor implements ToolExecutor {

	private static final Logger log = LoggerFactory.getLogger(McpToolExecutor.class);

	private final ToolSpecification toolSpecification;

	private final McpClient mcpClient;

	public McpToolExecutor(McpClient mcpClient, ToolSpecification toolSpecification) {
		this.mcpClient = mcpClient;
		this.toolSpecification = toolSpecification;
	}


	public String name() {
		return toolSpecification.name();
	}

	public ToolSpecification toolSpecification() {
		return toolSpecification;
	}

	@Override
	public String execute(ToolExecutionRequest toolExecutionRequest, Object memoryId) {


		try {
			String result = mcpClient.executeTool(toolExecutionRequest);
			log.debug("Tool execution result: {}", result);
			return result;
		} catch (RuntimeException cause) {
			log.error("Error while executing tool", cause);
			return cause.getMessage();
		}
	}
}
